package com.accp.dao.impl;

import java.util.ArrayList;
import java.util.List;
import org.hibernate.HibernateException;
import org.hibernate.Query;

import com.accp.dao.EmailDao;
import com.accp.pojo.Email;



public class EmailDaoImpl extends BaseDaoImpl implements EmailDao {
	/**
	 * 查询收件人
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<String> recipientsList() {
		List<String> list = new ArrayList<String>();
		try {
			list= getSession().createQuery("select userName from Users").list();
		} catch (HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 添加(发送)邮件
	 */
	@Override
	public boolean add(Email email) {
		boolean flag = false;
		try{
			getSession().save(email);
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 查收邮件
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Email> findByUserName(String userName) {
		List<Email> list = new ArrayList<Email>();
		try{
			String hql = "from Email where RECIPIENTS =:userName and isDeleted =1";
			list =  getSession().createQuery(hql).setString("userName", userName).list();
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 删除邮件
	 */
	@Override
	public boolean delete(Email email) {
		boolean flag = false;
		try{
			getSession().update(email);;
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 彻底删除邮件
	 */
	@Override
	public boolean deleteReal(Email email) {
		boolean flag = false;
		try{
			getSession().delete(email);
			flag = true;	
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 还原邮件
	 */
	@Override
	public boolean updateIsDeleted(Email email) {
		boolean flag = false;
		try{
			getSession().update(email);
			flag = true;
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}

	/**
	 * 查询垃圾邮件
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Email> spamList(String userName) {
		List<Email> list = new ArrayList<Email>();
		try{
			String hql = "from Email where recipients =:userName and isDeleted =0";
			list =  getSession().createQuery(hql).setString("userName", userName).list();
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		return list;
	}
	/**
	 * 修改邮件的读取状态
	 */
	@Override
	public boolean updateIsRead(Email email) {
		boolean flag = false;
		try{
			 getSession().update(email);
			flag = true;
		}catch(HibernateException e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 通过id查看邮件信息
	 */
	@Override
	public Email findById(Integer id) {
		Email email = null;
		try{
			email = (Email) getSession().get(Email.class,new Integer(id));
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return email;
	}
	/**
	 * 删除某个用户的全部邮件（当删除用户时）
	 */
	@Override
	public boolean deleteAllByUser(String userName) {
		boolean flag = false;
		System.out.println("传来的参数为： "+userName);
		try{
			String hql = "delete from Email where RECIPIENTS =:userName";
			getSession().createQuery(hql).setString("userName", userName).executeUpdate();
			flag = true;	
		}catch(HibernateException e){
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 分页查询邮件，获取总页数
	 */
	@Override
	public int totalPage(String className,String userName) {
		String hql = "select count(*) from "+ className + " where RECIPIENTS =:userName and isDeleted =1";
		Query query = this.getSession().createQuery(hql).setString("userName", userName);
		int total = Integer.parseInt(query.list().get(0).toString());
		return total;
	}
	/**
	 * 分页查询，获取每页的记录
	 */
	@SuppressWarnings("unchecked")
	@Override
	public List<Email> selectByPage(String className, int pageNo, int pageSize,String userName) {
		Query query = this.getSession().createQuery("from "+ className + " where RECIPIENTS =:userName and isDeleted =1")
				.setString("userName", userName);
		query.setFirstResult((pageNo - 1) * pageSize);//每页显示的第一条记录
		query.setMaxResults(pageSize);//每页显示的记录数
		return query.list();
	}

}


